package com.base.util;

import com.base.CoreConstants;
import com.base.crypto.Des;
import com.base.crypto.Rsa;
import com.base.lang.Base64;
import sun.applet.Main;

public class SecureUtil {

    private static Rsa rsa;
    private static Des des;

    public SecureUtil() {
    }

    public static String encrypt(String str) {
        return CoreConstants.ENCRYPT.equals(SecureUtil.encrypt.DES.name()) ? des.encrypt(str) : rsa(str);
    }

    public static String decrypt(String str){
        try {
            return CoreConstants.ENCRYPT.equals(encrypt.DES.name()) ? des.decrypt(str) : rsaDecrypt(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String rsa(String str) {
        try {
            byte[] b = rsa.encryptByPrivateKey(str.getBytes());
            return Base64.encode(b);
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    private static String rsaDecrypt(String str){
        try {
            byte[] b = rsa.decryptByPublicKey(Base64.decode(str));
            return new String(b);
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    static {
        rsa = new Rsa(CoreConstants.ENCRYPT_PRIVATE_KEY, CoreConstants.ENCRYPT_PUBLIC_KEY);
        des = new Des(CoreConstants.ENCRYPT_PRIVATE_KEY);
    }

    private static enum encrypt {
        DES,
        RSA;

        private encrypt() {
        }
    }


}
